package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

import com.ruoyi.common.utils.CollectionUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AlpElementCalc;
import com.ruoyi.system.mapper.AlpWeeklyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AlpMonthlyMapper;
import com.ruoyi.system.domain.AlpMonthly;
import com.ruoyi.system.service.IAlpMonthlyService;
import com.ruoyi.common.core.text.Convert;

/**
 * 月度汇总表Service业务层处理
 * 
 * @author zhangjun
 * @date 2020-02-21
 */
@Service
public class AlpMonthlyServiceImpl implements IAlpMonthlyService 
{
    @Autowired
    private AlpMonthlyMapper alpMonthlyMapper;

    @Autowired
    private AlpWeeklyMapper alpWeeklyMapper;

    /**
     * 查询月度汇总表
     * 
     * @param id 月度汇总表ID
     * @return 月度汇总表
     */
    @Override
    public AlpMonthly selectAlpMonthlyById(Long id)
    {
        return alpMonthlyMapper.selectAlpMonthlyById(id);
    }

    /**
     * 查询月度汇总表
     *
     * @param monthId
     * @return 月度汇总表
     */
    @Override
    public List<AlpElementCalc> selectAlpMonthlybyMonthId(int monthId)
    {
        return alpMonthlyMapper.selectAlpMonthlybyMonthId(monthId);
    }
    /**
     * 查询月度汇总表列表
     * 
     * @param alpMonthly 月度汇总表
     * @return 月度汇总表
     */
    @Override
    public List<AlpMonthly> selectAlpMonthlyList(AlpMonthly alpMonthly)
    {
        return alpMonthlyMapper.selectAlpMonthlyList(alpMonthly);
    }

    /**
     * 新增月度汇总表
     * 
     * @param alpMonthly 月度汇总表
     * @return 结果
     */
    @Override
    public int insertAlpMonthly(AlpMonthly alpMonthly)
    {
        return alpMonthlyMapper.insertAlpMonthly(alpMonthly);
    }

    /**
     * 修改月度汇总表
     * 
     * @param alpMonthly 月度汇总表
     * @return 结果
     */
    @Override
    public int updateAlpMonthly(AlpMonthly alpMonthly)
    {
        return alpMonthlyMapper.updateAlpMonthly(alpMonthly);
    }

    /**
     * 删除月度汇总表对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAlpMonthlyByIds(String ids)
    {
        return alpMonthlyMapper.deleteAlpMonthlyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除月度汇总表信息
     * 
     * @param id 月度汇总表ID
     * @return 结果
     */
    @Override
    public int deleteAlpMonthlyById(Long id)
    {
        return alpMonthlyMapper.deleteAlpMonthlyById(id);
    }


    @Override
    public List<Map> getTotalMonthlyList() {
        List<Map> list = alpMonthlyMapper.getTotalMonthlyList();
        Map budgetMap = new HashMap<>();
        budgetMap.put("type","预算");
        Map predictMap = new HashMap<>();
        predictMap.put("type","预测");
        Map actualMap = new HashMap<>();
        actualMap.put("type","结果");
        Map budgetPercentMap = new HashMap<>();
        budgetPercentMap.put("type","预算准确度");
        Map predictPercentMap = new HashMap<>();
        predictPercentMap.put("type","预测准确度");
        //2020-3-13添加，每类统计都添加合计列
        BigDecimal totalBudget = new BigDecimal(0);
        BigDecimal totalPredict = new BigDecimal(0);
        BigDecimal totalActual = new BigDecimal(0);
        DecimalFormat df = new DecimalFormat("0.00%");
        for(int i = 0; i <= list.size(); i++) {
            if(i < list.size()) {
                Map map = list.get(i);
                //计算预测准确率
                BigDecimal bMoney = (BigDecimal) map.get("budget_money");
                BigDecimal pMoney = (BigDecimal) map.get("predict_money");
                BigDecimal aMoney = (BigDecimal) map.get("actual_money");
                String budgetPercent;
                String predictPercent;
                if (bMoney.compareTo(BigDecimal.ZERO) == 0) {
                    budgetPercent = "0.00%";
                } else {
                    BigDecimal budgetPercentResult = aMoney.divide(bMoney, 4, BigDecimal.ROUND_HALF_UP);
                    budgetPercent = df.format(budgetPercentResult);
                }
                if (pMoney.compareTo(BigDecimal.ZERO) == 0) {
                    predictPercent = "0.00%";
                } else {
                    BigDecimal predictPercentResult = aMoney.divide(pMoney, 4, BigDecimal.ROUND_HALF_UP);
                    predictPercent = df.format(predictPercentResult);
                }
                budgetMap.put(String.valueOf(i + 1), map.get("budget_money"));
                predictMap.put(String.valueOf(i + 1), map.get("predict_money"));
                actualMap.put(String.valueOf(i + 1), map.get("actual_money"));
                budgetPercentMap.put(String.valueOf(i + 1), budgetPercent);
                predictPercentMap.put(String.valueOf(i + 1), predictPercent);
                //2020-3-13新加合计模块
                totalBudget = totalBudget.add(bMoney);
                totalActual = totalActual.add(aMoney);
                Calendar calendar = Calendar.getInstance();
                int month = calendar.get(Calendar.MONTH) + 1;
                if(i < month-1){
                    totalPredict = totalPredict.add(aMoney);
                }else{
                    totalPredict = totalPredict.add(pMoney);
                }
            } else {
                budgetMap.put(String.valueOf(i + 1), totalBudget);
                predictMap.put(String.valueOf(i + 1), totalPredict);
                actualMap.put(String.valueOf(i + 1), totalActual);
                if (totalBudget.compareTo(BigDecimal.ZERO) == 0) {
                    budgetPercentMap.put(String.valueOf(i + 1), "0.00%");
                } else {
                    BigDecimal budgetPercentResult = totalActual.divide(totalBudget, 4, BigDecimal.ROUND_HALF_UP);
                    budgetPercentMap.put(String.valueOf(i + 1), df.format(budgetPercentResult));
                }
                if (totalPredict.compareTo(BigDecimal.ZERO) == 0) {
                    predictPercentMap.put(String.valueOf(i + 1), "0.00%");
                } else {
                    BigDecimal predictPercentResult = totalActual.divide(totalPredict, 4, BigDecimal.ROUND_HALF_UP);
                    predictPercentMap.put(String.valueOf(i + 1), df.format(predictPercentResult));
                }
            }
        }
        List<Map> resultList = new ArrayList<>();
        resultList.add(budgetMap);
        resultList.add(predictMap);
        resultList.add(actualMap);
        resultList.add(budgetPercentMap);
        resultList.add(predictPercentMap);
        return resultList;
    }

    @Override
    public List<Map> getDeptMonthlyList(String deptNames) {
        List<String> nameList = new ArrayList<>();
        List<Map> resultList = new ArrayList<>();
        List<String> nameListFromDB = alpWeeklyMapper.getAllDeptName();
        if(StringUtils.isNotEmpty(deptNames)) {
            String[] personNameList = deptNames.split(",");
            for (String pn : personNameList) {
                List searchedList = CollectionUtils.search(pn, nameListFromDB);
                nameList.addAll(searchedList);
            }
        }
        if(nameList.size() > 0){
            for(String name: nameList){
                List<Map> list = alpMonthlyMapper.getDeptMonthlyList(name);
                Map budgetMap = new HashMap<>();
                budgetMap.put("type","预算");
                budgetMap.put("name",name);
                Map predictMap = new HashMap<>();
                predictMap.put("type","预测");
                predictMap.put("name",name);
                Map actualMap = new HashMap<>();
                actualMap.put("type","结果");
                actualMap.put("name",name);
                Map budgetPercentMap = new HashMap<>();
                budgetPercentMap.put("type","预算准确度");
                budgetPercentMap.put("name",name);
                Map predictPercentMap = new HashMap<>();
                predictPercentMap.put("type","预测准确度");
                predictPercentMap.put("name",name);
                //2020-3-13添加，每类统计都添加合计列
                BigDecimal totalBudget = new BigDecimal(0);
                BigDecimal totalPredict = new BigDecimal(0);
                BigDecimal totalActual = new BigDecimal(0);
                DecimalFormat df = new DecimalFormat("0.00%");
                for(int i = 0; i <= list.size(); i++) {
                    if(i < list.size()) {
                        Map map = list.get(i);
                        //计算预测准确率
                        BigDecimal bMoney = (BigDecimal) map.get("budget_money");
                        BigDecimal pMoney = (BigDecimal) map.get("predict_money");
                        BigDecimal aMoney = (BigDecimal) map.get("actual_money");
                        String budgetPercent;
                        String predictPercent;
                        if (bMoney.compareTo(BigDecimal.ZERO) == 0) {
                            budgetPercent = "0.00%";
                        } else {
                            BigDecimal budgetPercentResult = aMoney.divide(bMoney, 4, BigDecimal.ROUND_HALF_UP);
                            budgetPercent = df.format(budgetPercentResult);
                        }
                        if (pMoney.compareTo(BigDecimal.ZERO) == 0) {
                            predictPercent = "0.00%";
                        } else {
                            BigDecimal predictPercentResult = aMoney.divide(pMoney, 4, BigDecimal.ROUND_HALF_UP);
                            predictPercent = df.format(predictPercentResult);
                        }
                        budgetMap.put(String.valueOf(i + 1), map.get("budget_money"));
                        predictMap.put(String.valueOf(i + 1), map.get("predict_money"));
                        actualMap.put(String.valueOf(i + 1), map.get("actual_money"));
                        budgetPercentMap.put(String.valueOf(i + 1), budgetPercent);
                        predictPercentMap.put(String.valueOf(i + 1), predictPercent);

                        totalBudget = totalBudget.add(bMoney);
                        totalActual = totalActual.add(aMoney);
                        Calendar calendar = Calendar.getInstance();
                        int month = calendar.get(Calendar.MONTH) + 1;
                        if(i < month-1){
                            totalPredict = totalPredict.add(aMoney);
                        }else{
                            totalPredict = totalPredict.add(pMoney);
                        }
                    } else {
                        budgetMap.put(String.valueOf(i + 1), totalBudget);
                        predictMap.put(String.valueOf(i + 1), totalPredict);
                        actualMap.put(String.valueOf(i + 1), totalActual);
                        if (totalBudget.compareTo(BigDecimal.ZERO) == 0) {
                            budgetPercentMap.put(String.valueOf(i + 1), "0.00%");
                        } else {
                            BigDecimal budgetPercentResult = totalActual.divide(totalBudget, 4, BigDecimal.ROUND_HALF_UP);
                            budgetPercentMap.put(String.valueOf(i + 1), df.format(budgetPercentResult));
                        }
                        if (totalPredict.compareTo(BigDecimal.ZERO) == 0) {
                            predictPercentMap.put(String.valueOf(i + 1), "0.00%");
                        } else {
                            BigDecimal predictPercentResult = totalActual.divide(totalPredict, 4, BigDecimal.ROUND_HALF_UP);
                            predictPercentMap.put(String.valueOf(i + 1), df.format(predictPercentResult));
                        }
                    }
                }
                resultList.add(budgetMap);
                resultList.add(predictMap);
                resultList.add(actualMap);
                resultList.add(budgetPercentMap);
                resultList.add(predictPercentMap);
            }
        }
        return resultList;
    }

    @Override
    public List<Map> getPersonalMonthlyList(String personNames) {
        List<String> nameList = new ArrayList<>();
        List<Map> resultList = new ArrayList<>();
        List<String> nameListFromDB = alpWeeklyMapper.getAllXsName();
        if(StringUtils.isNotEmpty(personNames)) {
            String[] personNameList = personNames.split(",");
            for (String pn : personNameList) {
                List searchedList = CollectionUtils.search(pn, nameListFromDB);
                nameList.addAll(searchedList);
            }
        }
        if(nameList.size() > 0){
            for(String name: nameList){
                List<Map> list = alpMonthlyMapper.getPersonalMonthlyList(name);
                Map budgetMap = new HashMap<>();
                budgetMap.put("type","预算");
                budgetMap.put("name",name);
                Map predictMap = new HashMap<>();
                predictMap.put("type","预测");
                predictMap.put("name",name);
                Map actualMap = new HashMap<>();
                actualMap.put("type","结果");
                actualMap.put("name",name);
                Map budgetPercentMap = new HashMap<>();
                budgetPercentMap.put("type","预算准确度");
                budgetPercentMap.put("name",name);
                Map predictPercentMap = new HashMap<>();
                predictPercentMap.put("type","预测准确度");
                predictPercentMap.put("name",name);
                //2020-3-13添加，每类统计都添加合计列
                BigDecimal totalBudget = new BigDecimal(0);
                BigDecimal totalPredict = new BigDecimal(0);
                BigDecimal totalActual = new BigDecimal(0);
                DecimalFormat df = new DecimalFormat("0.00%");
                for(int i = 0; i <= list.size(); i++) {
                    if(i<list.size()){
                        Map map = list.get(i);
                        //计算预测准确率
                        BigDecimal bMoney = (BigDecimal)map.get("budget_money");
                        BigDecimal pMoney = (BigDecimal)map.get("predict_money");
                        BigDecimal aMoney = (BigDecimal)map.get("actual_money");
                        String budgetPercent;
                        String predictPercent;
                        if(bMoney.compareTo(BigDecimal.ZERO) == 0) {
                            budgetPercent = "0.00%";
                        } else {
                            BigDecimal budgetPercentResult = aMoney.divide(bMoney,4,BigDecimal.ROUND_HALF_UP);
                            budgetPercent = df.format(budgetPercentResult);
                        }
                        if(pMoney.compareTo(BigDecimal.ZERO) == 0) {
                            predictPercent = "0.00%";
                        } else {
                            BigDecimal predictPercentResult = aMoney.divide(pMoney,4,BigDecimal.ROUND_HALF_UP);
                            predictPercent = df.format(predictPercentResult);
                        }
                        budgetMap.put(String.valueOf(i+1), map.get("budget_money"));
                        predictMap.put(String.valueOf(i+1), map.get("predict_money"));
                        actualMap.put(String.valueOf(i+1), map.get("actual_money"));
                        budgetPercentMap.put(String.valueOf(i+1), budgetPercent);
                        predictPercentMap.put(String.valueOf(i+1), predictPercent);
                        totalBudget = totalBudget.add(bMoney);
                        totalActual = totalActual.add(aMoney);
                        Calendar calendar = Calendar.getInstance();
                        int month = calendar.get(Calendar.MONTH) + 1;
                        if(i < month-1){
                            totalPredict = totalPredict.add(aMoney);
                        }else{
                            totalPredict = totalPredict.add(pMoney);
                        }
                    } else {
                        budgetMap.put(String.valueOf(i + 1), totalBudget);
                        predictMap.put(String.valueOf(i + 1), totalPredict);
                        actualMap.put(String.valueOf(i + 1), totalActual);
                        if (totalBudget.compareTo(BigDecimal.ZERO) == 0) {
                            budgetPercentMap.put(String.valueOf(i + 1), "0.00%");
                        } else {
                            BigDecimal budgetPercentResult = totalActual.divide(totalBudget, 4, BigDecimal.ROUND_HALF_UP);
                            budgetPercentMap.put(String.valueOf(i + 1), df.format(budgetPercentResult));
                        }
                        if (totalPredict.compareTo(BigDecimal.ZERO) == 0) {
                            predictPercentMap.put(String.valueOf(i + 1), "0.00%");
                        } else {
                            BigDecimal predictPercentResult = totalActual.divide(totalPredict, 4, BigDecimal.ROUND_HALF_UP);
                            predictPercentMap.put(String.valueOf(i + 1), df.format(predictPercentResult));
                        }
                    }
                }
                resultList.add(budgetMap);
                resultList.add(predictMap);
                resultList.add(actualMap);
                resultList.add(budgetPercentMap);
                resultList.add(predictPercentMap);
            }
        }
        return resultList;
    }

    public static void main(String[] args) {
        Calendar calendar = Calendar.getInstance();
        System.out.println(calendar.get(Calendar.MONTH) + 1);
    }
}
